package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.PrisTeam1Car;

import manager.CarManager;
import manager.CommentManager;

/**
 * Servlet implementation class NewCommentServlet
 */
@WebServlet("/NewCommentServlet")
public class NewCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewCommentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String text = request.getParameter("test");
		String id = request.getParameter("id");
		CarManager cm = new CarManager();
		PrisTeam1Car car = cm.getCarById(id);

		String msg;

		CommentManager cmm = new CommentManager();
		boolean ok = cmm.createAndStoreComment(car, text);
		if (ok) {
			response.sendRedirect("web/ad.jsp");
		} else {
			msg = "Entry has NOT been succesfull, please try again !!!";
			request.getSession().setAttribute("message", msg);
			response.sendRedirect("web/addComment.jsp");
		}
	}

}
